package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        //try();自动关闭特性，当try中代码执行完毕后，会自动调运该对象的close()方法;
        //断开数据库的连接，不用再connection.close()手动断开连接了;
       try( Connection connection = DBUtil.getConnection();){

           Statement statement = connection.createStatement();
           String sql = "SELECT username ,password,age From userinfo";
           ResultSet resultSet = statement.executeQuery(sql);
            /*resultSet.next():判断是否存在下一条记录
            指针的起始位置，在第一个元素之前
             1.存在：返回true
             2.不存在：返回false
             */


           while(resultSet.next()){
               String username = resultSet.getString("username");
               String password = resultSet.getString("password");
               int age = resultSet.getInt("age");
               System.out.println(username+" "+password+" "+age);
           }

       }catch (SQLException e)
       {
           throw new RuntimeException(e);
       }

    }
}
